package com.carl.zlds.tool.validator;

import com.carl.zlds.tool.util.RegExUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.carl.zlds.tool.validator.Phone;
import org.apache.commons.lang3.StringUtils;

public class PhoneValidator implements ConstraintValidator<com.carl.zlds.tool.validator.Phone, String> {
    private boolean required = false;

    public PhoneValidator() {
    }

    public void initialize(Phone constraintAnnotation) {
        this.required = constraintAnnotation.required();
    }

    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (this.required) {
            return RegExUtil.isMobileSimple(value);
        } else {
            return StringUtils.isEmpty(value) ? true : RegExUtil.isMobileSimple(value);
        }
    }
}
